package com.example.mobile2021606128wje0601;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


    private EditText edtTxtMainUserName;
    private EditText edtTxtMainPassword;
    private CheckBox chkBoxMainPSW;
    private CheckBox chkBoxMainName;
    private Button btnMainLogin;
    private Button btnMainReset;

    private SharedPreferences sp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        sp = getSharedPreferences("login",MODE_PRIVATE);
        //复选框监听器
        chkBoxMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();//sp编辑器
                editor.putBoolean("chkName",isChecked);//将选中状态 存入 sp
                editor.commit();//提交

            }
        });
        chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("chkPwd",isChecked);
                editor.commit();
            }
        });
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //存储
                String userName=edtTxtMainUserName.getText().toString().trim();
                String passWord=edtTxtMainPassword.getText().toString().trim();
                if (userName.equals("admin")&&passWord.equals("123456")){
                    if (chkBoxMainName.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();
                        editor.putString("userName",userName);
                        editor.commit();
                        if (chkBoxMainPSW.isChecked()){
                            SharedPreferences.Editor editor1=sp.edit();
                            editor.putString("passWord",passWord);
                            editor1.commit();
                        }
                    }
                }

            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkName=sp.getBoolean("chkName",false);
        chkBoxMainName.setChecked(chkName);

        //获取，赋值给控件
        if (chkName){
            edtTxtMainUserName.setText(sp.getString("userName",null));
        }

    }

    private void initView() {
        edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
        edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
        chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
        chkBoxMainName = (CheckBox) findViewById(R.id.chkBoxMainName);
        btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
        btnMainReset = (Button) findViewById(R.id.btnMainReset);
    }
}